/*
 * Accel.cpp
 *
 *  Created on: Jul 1, 2012
 *      Author: jmurray
 */

#include "Accel.h"
#include "Printf.h"

Accel::Accel() {
   x=0;
   y=0;
   z=0;
   temperature=0;
   LastUpdate=0;
   Wire.begin();
}

bool Accel::setTune(){
   //char optBuf;
   Wire.beginTransmission((ACCEL_I2C_ADDRESS));
   Wire.write(0x2E);
   Wire.endTransmission();

   Wire.requestFrom((ACCEL_I2C_ADDRESS | I2C_READ_MODE),13);

   while(Wire.available()!=0)
      p("Tune: 0x%x\n",Wire.read());


   return( true );
}

bool Accel::update( ){

   /*
    *
    * Timing notes:
    *    At r23 this code takes 1189.50 us.
    *    At r19 this code takes 1204.65 us.
    */
   Wire.beginTransmission((ACCEL_I2C_ADDRESS));
   Wire.write(ACCEL_REG_X_LSB);
   Wire.endTransmission();

   Wire.requestFrom((ACCEL_I2C_ADDRESS | I2C_READ_MODE),7);

   while(Wire.available()!=7);
   /*
    * This will be remade to assume the number is just bigger. We will use the
    *    full upper range of the number for the values give, and leave the
    *    bottom two bits = 0. Thanks Cam!
    */
   x =  Wire.read() & ACCEL_MASK_LSB;
   x |= Wire.read()<<8;
   y =  Wire.read() & ACCEL_MASK_LSB;
   y |= Wire.read()<<8;
   z =  Wire.read() & ACCEL_MASK_LSB;
   z |= Wire.read()<<8;
   temperature = Wire.read();

   return( true );
}

